/* Copyright (c) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

orkutdevbox.SNIPPETS = [
   { section: 'Environment' },
   
   { name: 'Register OnLoad Handler', 
     code: 'gagdets.util.registerOnLoadHandler(init)' },


   { section: 'OS IdSpecs' },

   { name: 'IdSpec: Viewer (string)', 
     code: 'opensocial.IdSpec.PersonId.VIEWER' },
   { name: 'IdSpec: Owner (string)', 
     code: 'opensocial.IdSpec.PersonId.OWNER' },
   { name: 'IdSpec: Viewer (object)', 
     code: 'var idspec = opensocial.newIdSpec({\'userId\':\'VIEWER\'});' },
   { name: 'IdSpec: Owner (object)', 
     code: 'var idspec = opensocial.newIdSpec({\'userId\':\'OWNER\'});' },
   { name: 'IdSpec: viewer\'s friends',
     code: 'var idspec = opensocial.newIdSpec({ "userId" : "VIEWER", "groupId" : "FRIENDS" });'},
   { name: 'IdSpec: owner\'s friends',
     code: 'var idspec = opensocial.newIdSpec({ "userId" : "OWNER", "groupId" : "FRIENDS" });'},


   { section: 'Data Requests'},

   { name: 'OS data request',
     code: 'var req = opensocial.newDataRequest();' },

   { name: 'OS request send',
     code: 'req.send(callback);' },
   
   { name: 'OS callback',
     code:   'function callback(data) {\n'
             + '   if (data.hadError()) {\n'
             + '      // ...\n'
             + '      return;\n'
             + '   }\n'
             + '\n'
             + '   var data1 = data.get("my_id_here").getData();\n'
             + '   var data2 = data.get("my_other_id_here").getData();\n\n'
             + '   // ...\n'
             + '}' },


   { section: 'Person'},

   { name: 'OS fetch person request',
     code: 'req.add(req.newFetchPersonRequest("VIEWER", "my_id_here");' },
   
   { name: 'OS fetch person request w/ params',
     code: 'req.add(req.newFetchPersonRequest("VIEWER",optparam),"my_id_here");' },
   
   { name: 'Request extra fields',
     code: 'optparam[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = ['+
    '   opensocial.Person.Field.ABOUT_ME,\n' +
    '   opensocial.Person.Field.THUMBNAIL_URL\n' +
    '];' },

   { name: 'Get person field',
     code: 'person.getField(opensocial.Person.Field.ABOUT_ME)' },
   
   { section: 'Person Fields'},
   
   { name: 'About Me', 
     code: 'person.getField(opensocial.Person.Field.ABOUT_ME)' },
   { name: 'Addresses',
     code: 'person.getField( opensocial.Person.Field.ADDRESSES)' },
   { name: 'Books', 
     code: 'person.getField(nsocial.Person.Field.BOOKS)' },
   { name: 'Current Location', 
     code: 'person.getField(opensocial.Person.Field.CURRENT_LOCATION)' },
   { name: 'Fashion', 
     code: 'person.getField(opensocial.Person.Field.FASHION)' },
   { name: 'Food', 
     code: 'person.getField(opensocial.Person.Field.FOOD)' },
   { name: 'Gender', 
     code: 'person.getField(opensocial.Person.Field.GENDER)' },
   { name: 'Humor', 
     code: 'person.getField(opensocial.Person.Field.HUMOR)' },
   { name: 'Interests', 
     code: 'person.getField(opensocial.Person.Field.INTERESTS)' },
   { name: 'Languages', 
     code: 'person.getField(opensocial.Person.Field.LANGUAGES_SPOKEN)' },
   { name: 'Living Arrangement', 
     code: 'person.getField(opensocial.Person.Field.LIVING_ARRANGEMENT)' },
   { name: 'Movies', 
     code: 'person.getField(opensocial.Person.Field.MOVIES)' },
   { name: 'Music', 
     code: 'person.getField(opensocial.Person.Field.MUSIC)' },
   { name: 'Name', 
     code: 'person.getField(opensocial.Person.Field.NAME)' },
   { name: 'Profile URL', 
     code: 'person.getField(opensocial.Person.Field.PROFILE_URL)' },
   { name: 'Relationship Status', 
     code: 'person.getField(opensocial.Person.Field.RELATIONSHIP_STATUS)' },
   { name: 'Sports', 
     code: 'person.getField(opensocial.Person.Field.SPORTS)' },
   { name: 'Status', 
     code: 'person.getField(opensocial.Person.Field.STATUS)' },
   { name: 'Thumbnail URL', 
     code: 'person.getField(opensocial.Person.Field.THUMBNAIL_URL)' },
   { name: 'Turn-ons', 
     code: 'person.getField(opensocial.Person.Field.TURN_ONS)' },
   { name: 'TV Shows', 
     code: 'person.getField(opensocial.Person.Field.TV_SHOWS)' },
   { name: 'URLs', 
     code: 'person.getField(opensocial.Person.Field.URLS)' },
   

   { section: 'People'},
   
   { name: 'Fetch People Request',
     code: 'req.add(req.newFetchPeopleRequest(idspec, opt_params), \'viewerFriends\');'},

   { name: 'IdSpec: viewer\'s friends',
     code: 'var idspec = opensocial.newIdSpec({ "userId" : "VIEWER", "groupId" : "FRIENDS" });'},
   
   { name: 'IdSpec: owner\'s friends',
     code: 'var idspec = opensocial.newIdSpec({ "userId" : "OWNER", "groupId" : "FRIENDS" });'},

   { name: 'Set Max Results',
     code: 'optparams[opensocial.DataRequest.PeopleRequestFields.MAX] = 20;'},
   
   { name: 'Retrieve result as array',
     code: 'var friends = data.get(\'viewerFriends\').getData().asArray();' },
   
   { name: 'Filter: only friends w/ app',
     code: 'optparams["filter"] = "hasApp";'},
   
   { name: 'Set pagination range',
     code: 'param[opensocial.DataRequest.PeopleRequestFields.FIRST] = first_index_here;\n' +
           'param[opensocial.DataRequest.PeopleRequestFields.MAX] = page_size_here;' },
  

   { section: 'AppData' },

   { name: 'Fetch App Data Request',
     code: 'req.add(req.newFetchPersonAppDataRequest(idspec_object, ' + 
           '[ "fields", "go", "here" ]), "appdata");' },

   { name: 'Read App Data Response',
     code: 'var appData = data.get(\'my_id\').getData();\n' +
           'if (typeof appData == \'undefined\' || appData == null) {\n' +
           '   // no data\n' +
           '}\n' +
           'var viewerData = appData[viewer.getId()];' },
   
   { name: 'Read App Data Fields',
     code: 'var favcolor = appData[viewer.getId()]["favcolor"];' },
   
   { name: 'Update App Data Request',
     code: 'req.add(req.newUpdatePersonAppDataRequest(' +
           '"VIEWER", "favcolor", "purple"));' },

   { name: 'Remove App Data Request',
     code: 'req.add(req.newRemovePersonAppDataRequest(' + 
           '"VIEWER", [ "fields", "go", "here" ]), "data");' },
   
   
   { section: 'Activities and Messages' },

   { name: 'Create Activity Request',
     code: 'var params = {};\n' +
           'params[opensocial.Activity.Field.TITLE] = "This is the <b>activity title</b>."\n' +
           'params[opensocial.Activity.Field.BODY] = "This is the <em>activity body.</em>"\n' +
           'opensocial.requestCreateActivity(opensocial.newActivity(params),\n' +
           '     opensocial.CreateActivityPriority.HIGH, callback);' },

   { name: 'Send Message Request',
     code: 'var friendId = /* friend\'s OpenSocial ID here */;\n' +
           'var params = {};\n' +
           'params[opensocial.Message.Field.TITLE] = \'Message subject here\';\n' +
           'params[opensocial.Message.Field.BODY] = \'Message body here.\';\n' +
           'opensocial.requestSendMessage(friendId, opensocial.newMessage(params), callback);' },

   { section: 'Remote Server' },

   { name: 'Remote Server request/reply',
     code: 'function init() {\n' +
           '   var params = {};\n' +
           '   params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;\n' +
           '   gadgets.io.makeRequest(MY_URL, callback, params);\n' +
           '   debug.say("Request sent. Waiting for reply...");\n' +
           '}\n' +
           '\n' +
           'function callback(data) {\n' +
           '   debug.say("Reply received.");\n' +
           '   if (data.data && data.data.length) {\n' +
           '      // ...do something with data.data here...\n' +
           '   }\n' +
           '}\n' },



   { section: 'Albums'},

   { name: 'Fetch Albums Request',
     code: 'req.add(req.newFetchAlbumsRequest(idspec),\'albums\');' },
   
   { name: 'Get album response',
     code: 'var albums = resp.get(\'albums\').getData().asArray();' },
   
   { name: 'Get album fields',
     code: 'var title = alb.getField(opensocial.Album.Field.TITLE);\n' +
           'var desc = alb.getField(opensocial.Album.Field.DESCRIPTION);\n' +
           'var itemcount = alb.getField(opensocial.Album.Field.MEDIA_ITEM_COUNT);\n' +
           'var thumb = alb.getField(opensocial.Album.Field.THUMBNAIL_URL);' },

   { name: 'Create Album Request',
     code: 'var params = {};\n' +
           'params[opensocial.Album.Field.TITLE] = \'album title\';\n' +
           'var album = new opensocial.Album(params);\n' +
           'req.add(req.newCreateAlbumRequest(idspec, album), "album");\n' },

   { name: 'Update Album Request',
     code: 'var fields = [];\n' +
           'fields[opensocial.Album.Field.TITLE] = title;\n' +
           'fields[opensocial.Album.Field.DESCRIPTION] = desc;\n' +
           'req.add(req.newUpdateAlbumRequest(idspec, albumId, fields),\'update\');\n' },



   { section: 'Photos' },

   { name: 'Fetch Photos Request',
     code: 'var idspec = opensocial.newIdSpec({\'userId\':\'VIEWER\'});\n' +
           'var albumId = /* the ID of the album you want to list */\n' +
           'var req = opensocial.newDataRequest();\n' +
           'req.add(req.newFetchMediaItemsRequest(idspec, albumId), \'photos\');\n' },

   { name: 'Fetch Photos Response',
     code: 'var photos = resp.get(\'photos\').getData().asArray();' },
   


   { section: 'Error Handling' },

   { name: 'Test for Error on Callback',
     code:    'function callback(resp) {\n' +
              '   if (resp.hadError()) {\n' +
              '      handleError(resp, [\'id1\',\'id2\'] /* your IDs here */);\n' +
              '      return;\n' +
              '   }\n' +
              '   debug.say(\'success\');\n' +
              '}\n' },

   { name: 'Print Error to Debug Console',
     code: 'function handleError(resp, items) {\n' +
           '   var tlmsg = resp.getErrorMessage();\n' +
           '   if (typeof(tlmsg) == \'undefined\' || tlmsg == null) tlmsg = "";\n' +
           '\n' +
           '   debug.say("*** Error: " + tlmsg);\n' +
           '   \n' +
           '   for (var i in items) {\n' +
           '      var item = resp.get(items[i]);\n' +
           '      var prefix = "***   "  + items[i] + ": ";\n' +
           '\n' +
           '      if (typeof(item) == \'undefined\' || item == \'null\')\n' +
           '         debug.say(prefix + "[item unavailable]");\n' +
           '      else if (item.hadError())\n' +
           '         debug.say(prefix + "[" + item.getErrorCode() \n' +
           '                          + "] " + item.getErrorMessage());\n' +
           '   }\n' +
           '}\n' },


   { section: 'Orkut Environment' },

   { name: 'Get URL parameters',
     code: 'var urlParams = gadgets.util.getUrlParameters();' },

   { name: 'Get AppParams',
     code: 'var urlParams = gadgets.util.getUrlParameters();\n' +
           'var appParams = urlParams[\'view-params\'];' }
];

